// pages/map/index.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
const app = getApp()
Page({
  data: {
    address:"",
    addressList:''
  },
  msg(){
    if (!wx.getStorageSync('token')) {
      wx.showToast({
        title: '请先登录...',
        image: '../../static/icons/err.png',
        duration: 2000,
        success: function (res) {
          wx.navigateTo({
            url: '../my/loging/index',
          })
        }
      })
      return
    }
    wx.navigateTo({
      url: '../location/location' 
    })
  },
  reCheck(){
    const that = this
    qqmapsdk.reverseGeocoder({
      poi_options: 'policy=2',
      success: function (res) {
        that.setData({
          address: res.result.address_component.district
        })
      }
    })
  },
  // 编辑
  edit(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../location/location?id='+id,
    })
  },
  onClose(e){
    const { position, instance } = e.detail;
    var that = this
    if(position=="right"){
      wx.showModal({
        title: '提示',
        content: '确定删除吗?',
        success(res){
          if(res.confirm){
            var model = {
              token: wx.getStorageSync('token'),
              _id: e.currentTarget.dataset.id
            }
            app.api.deleteData('admin/address', model, res => {
              that.setData({
                addressList: res.data
              })
              instance.close();
            })
          }else if(res.cancel) {
            instance.close();
          }
        }
      })
    }
  },
  aa(e){
    let curPages = getCurrentPages()
    let prevPage = curPages[curPages.length - 2]
    if(prevPage.data.name == undefined){
      wx.showModal({
        title: '修改服务点？',
        content: e.currentTarget.dataset.local,
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if(result.confirm){
            wx.setStorageSync('service',e.currentTarget.dataset.local)
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 1500,
              mask: false
            })
          }
        },
      })
    }
  },
  // 改变地址
  changeAdress(e){
    let curPages = getCurrentPages()
    let prevPage = curPages[curPages.length - 2]
    let address = app.globalData.addressList.filter(item => {
      return item._id == e.currentTarget.dataset.id
    })
    if(prevPage.data.name != undefined){
      prevPage.setData({
        name:address[0].name,
        phone:address[0].phone,
        address:address[0].local
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },
  onLoad: function (options) {
    var _this = this
    qqmapsdk = new QQMapWX({
      key: 'J5NBZ-RA7KP-NYNDD-VNBCU-ICXHO-R6F3P'
    });
    _this.reCheck()
  },
  onShow(){
    var that = this;
    const token = wx.getStorageSync('token')
    if(!token){
      return
    }
    app.api.getData('admin/address', {token: token}, (res) => {
      that.setData({
        addressList: res.data
      })
      app.globalData.addressList=res.data
    })
  }
})